
package yunxiangchuan

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/yunxiangchuan"
    yunxiangchuanReq "github.com/flipped-aurora/gin-vue-admin/server/model/yunxiangchuan/request"
)

type VipsService struct {}
// CreateVips 创建会员等级管理记录
// Author [yourname](https://github.com/yourname)
func (vipsService *VipsService) CreateVips(ctx context.Context, vips *yunxiangchuan.Vips) (err error) {
	err = global.GVA_DB.Create(vips).Error
	return err
}

// DeleteVips 删除会员等级管理记录
// Author [yourname](https://github.com/yourname)
func (vipsService *VipsService)DeleteVips(ctx context.Context, id string) (err error) {
	err = global.GVA_DB.Delete(&yunxiangchuan.Vips{},"id = ?",id).Error
	return err
}

// DeleteVipsByIds 批量删除会员等级管理记录
// Author [yourname](https://github.com/yourname)
func (vipsService *VipsService)DeleteVipsByIds(ctx context.Context, ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]yunxiangchuan.Vips{},"id in ?",ids).Error
	return err
}

// UpdateVips 更新会员等级管理记录
// Author [yourname](https://github.com/yourname)
func (vipsService *VipsService)UpdateVips(ctx context.Context, vips yunxiangchuan.Vips) (err error) {
	err = global.GVA_DB.Model(&yunxiangchuan.Vips{}).Where("id = ?",vips.Id).Updates(&vips).Error
	return err
}

// GetVips 根据id获取会员等级管理记录
// Author [yourname](https://github.com/yourname)
func (vipsService *VipsService)GetVips(ctx context.Context, id string) (vips yunxiangchuan.Vips, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&vips).Error
	return
}
// GetVipsInfoList 分页获取会员等级管理记录
// Author [yourname](https://github.com/yourname)
func (vipsService *VipsService)GetVipsInfoList(ctx context.Context, info yunxiangchuanReq.VipsSearch) (list []yunxiangchuan.Vips, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&yunxiangchuan.Vips{})
    var vipss []yunxiangchuan.Vips
    // 如果有条件搜索 下方会自动创建搜索语句
    
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
         	orderMap["display_order"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&vipss).Error
	return  vipss, total, err
}
func (vipsService *VipsService)GetVipsPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
